package com.nefu.softlab.xjwc.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author : Ar1es
 * @date : 2020/1/12
 * @since : Java 8
 * 200：表示成功
 * 500：表示错误，错误信息在msg字段中
 * 501：bean验证错误，不管多少个错误都以map形式返回
 * 502：shiro安全认证
 * 503: 不具备角色功能
 * 555：异常抛出信息
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class JSONResult implements Serializable {
    private static final long serialVersionUID = 1L;

    private Integer resultCode;
    private String message;
    private Object data;


    public static JSONResult build(Integer status, String msg, Object data) {
        return new JSONResult(status, msg, data);
    }

    public static JSONResult ok(Object data) {
        return new JSONResult(data);
    }

    public static JSONResult ok() {
        return new JSONResult(null);
    }

    public static JSONResult errorMessage(String message) {
        return new JSONResult(500, message, null);
    }

    public static JSONResult errorMap(Object data) {
        return new JSONResult(501, "error", data);
    }

    public static JSONResult errorTokenMessage(String message) {
        return new JSONResult(502, message, null);
    }

    public static JSONResult errorRolesMessage(String message) {
        return new JSONResult(503, message, null);
    }

    public static JSONResult errorException(String message) {
        return new JSONResult(555, message, null);
    }

    public JSONResult(Object data) {
        this.resultCode = 200;
        this.message = "OK";
        this.data = data;
    }
}
